﻿using System;
using System.Web.Http;
using ML.IBusiness;
using ML.Entity;

namespace ML.WebApi.Controllers
{
    /// <summary>
    /// 商户api接口
    /// </summary>
    [CrossSite]
    public class MerchantController : BaseApiController
    {
        private readonly IMerchantBusiness merchantBusiness;
        public MerchantController(IMerchantBusiness merchantBusiness)
        {
            this.merchantBusiness = merchantBusiness;
        }

        /// <summary>
        /// 前端已经在使用此接口—根据平台分类查询营业中未删除商户列表
        /// </summary>
        /// <param name="id">平台分类Id</param>
        /// <param name="pageindex">第几页</param>
        /// <param name="pagesize">每页显示多少条</param>
        /// <returns></returns>
        [Route("api/merchant/getlistbyplatformid")]
        [HttpGet]
        public IHttpActionResult GetListByPlatFormId(string id, int pageindex, int pagesize)
        {
            var data = merchantBusiness.GetListByPlatFormId(id, pageindex, pagesize);
            return Json(data);
        }

        /// <summary>
        /// 前端已经在使用此接口—查询所有营业中未删除的商户列表
        /// </summary>
        /// <param name="name">商户名称</param>
        /// <param name="pageindex">第几页</param>
        /// <param name="pagesize">每页显示多少条</param>
        /// <returns></returns>
        [Route("api/merchant/getnodelbusinesslist")]
        [HttpGet]
        public IHttpActionResult GetNoDelBusinessList(int pageindex, int pagesize, string name = "")
        {
            var data = merchantBusiness.GetNoDelBusinessList(name, pageindex, pagesize);
            return Json(data);
        }

        /// <summary>
        /// 前端已经在使用此接口—根据商户Id获取商户信息
        /// </summary>
        /// <param name="id">商户Id</param>
        /// <returns></returns>
        [Route("api/merchant/getinfobyid")]
        [HttpGet]
        public IHttpActionResult GetInfoById(string id)
        {
            var data = merchantBusiness.GetInfoById(id);
            return Json(data);
        }

        /// <summary>
        ///  前端已经在使用此接口—根据商户Id查询产品分类和产品的组合信息(返回对象)
        /// </summary>
        /// <param name="id">商户Id</param>
        /// <returns></returns>
        [Route("api/merchant/getproductcategoryandinfobyid")]
        [HttpGet]
        public IHttpActionResult GetProductCategoryAndInfoById(string id)
        {
            var data = merchantBusiness.GetProductCategoryAndInfoById(id);
            return Json(data);
        }

        /// <summary>
        /// 更新店铺信息
        /// </summary>
        /// <returns></returns>
        [Route("api/wx/merchant")]
        [HttpPut]
        public IHttpActionResult UpdateMerchant(MerchantEntity entity)
        {
            var updateTime= DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:mm:ss");//由于泰国比中国慢1小时
            entity.UpdateTime=DateTime.Parse(updateTime);
            var data = merchantBusiness.Update(entity);
            return Json(data);
        }
    }
}